﻿@using FujiyBlog.Core.DomainObjects
@using FujiyBlog.Web.Models
@using FujiyBlog.Core.Extensions
@using FujiyBlog.Web.Extensions

@model IEnumerable<PostComment>

    @foreach (PostComment comment in Model)
    { 
        <div id="comment_id@(comment.Id)" class="comment@(comment.Post.Author == comment.Author ? " self" : "")">
            <p class="date">@FujiyBlog.Web.Common.DateTimeUtil.ConvertUtcToMyTimeZone(comment.CreationDate)</p>

            @if (!string.IsNullOrEmpty(Settings.SettingRepository.CommentsAvatar))
            {
                <p class="gravatar">
                    @Html.Gravatar(comment.Author == null ? comment.AuthorEmail : comment.Author.Email, 80, Settings.SettingRepository.CommentsAvatar)</p>
            }
            
            <p class="content">@Html.Raw(comment.Comment.Replace(Environment.NewLine, "<br />"))</p>

            @if (comment.Author == null)
            { 
                if (!string.IsNullOrEmpty(comment.AuthorWebsite))
                {
                    <a href="@comment.AuthorWebsite" target="_blank" rel="nofollow">@(comment.AuthorName)</a>
                }
                else
                {
                    <span>@comment.AuthorName</span>
                }
            }
            else { 
                @Html.ActionLink(comment.Author.DisplayName ?? comment.Author.Username, MVC.Post.Author(comment.Author.Username, null))
            }

            @if (User.IsInRole(Role.CreateComments))
            {
                <text>| <a href="javascript:void(0)" onclick="javascript:fujiyBlog.comments.replyComment(@comment.Id);">@Resources.Labels.Reply</a> </text>
            }
            
            @if (User.IsInRole(Role.ModerateComments))
            {
                
                @(Ajax.ActionLink("Approve", MVC.Comment.Approve(comment.Id), new AjaxOptions { HttpMethod = "POST", OnSuccess = "$('#link-approve-comment-" + comment.Id + "').fadeOut('normal', function() {$('#link-disapprove-comment-" + comment.Id + "').fadeIn('normal');});" }, new { id = "link-approve-comment-" + comment.Id, style = comment.IsApproved ? "display: none;" : string.Empty }))
                
                @(Ajax.ActionLink("Disapprove", MVC.Comment.Disapprove(comment.Id), new AjaxOptions { HttpMethod = "POST", OnSuccess = "$('#link-disapprove-comment-" + comment.Id + "').fadeOut('normal', function() {$('#link-approve-comment-" + comment.Id + "').fadeIn('normal');});" }, new { id = "link-disapprove-comment-" + comment.Id, style = !comment.IsApproved ? "display: none;" : string.Empty }))
                
                <text>|</text>
                @(Ajax.ActionLink("Delete", MVC.Comment.Delete(comment.Id, false), new AjaxOptions { HttpMethod = "POST", Confirm = "Sure?", OnSuccess = "$('#replies_" + comment.Id + " > *').insertAfter('#comment_id" + comment.Id + "');$('#comment_id" + comment.Id + "').slideUp(1000, function () {$(this).remove();});" }))
                if (comment.NestedComments.Any())
                {
                    <text>|</text>
                    @(Ajax.ActionLink("Delete this and replies", MVC.Comment.Delete(comment.Id, true), new AjaxOptions {HttpMethod = "POST", Confirm = "Sure?", OnSuccess = "$('#comment_id" + comment.Id + "').slideUp(1000, function () {$(this).remove();});"}))
                }
            }
            
            
            <div class="comment-replies" id="replies_@(comment.Id)">
                @{Html.RenderPartial("../Comment/Comments", comment.NestedComments);}
            </div>
        </div>
    }

